//
//  TravelsDetailTableViewCell.m
//  哎游
//
//  Created by 康守帅 on 15-1-5.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TravelsDetailTableViewCell.h"
#import "macro.h"
#import "Masonry.h"

@implementation TravelsDetailTableViewCell

- (void)dealloc
{
    [_dateImageView release];
    [_backImageView release];
    [_detail release];
    [_date release];
    [_view release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor grayColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.view = [[UIView alloc] init];
        [self.contentView addSubview:_view];
        [_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(super.mas_left).offset(15);
            make.top.equalTo(super.mas_top).offset(0);
            make.right.equalTo(super.mas_right).offset(-15);
            make.height.mas_equalTo(200);
        }];
        [_view release];
        
        self.backImageView = [[UIImageView alloc] init];
//        _backImageView.layer.masksToBounds = YES;
//        _backImageView.layer.cornerRadius = 10;
        [_view addSubview:_backImageView];
        [_backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_view.mas_left).offset(0);
            make.top.equalTo(_view.mas_top).offset(0);
            make.right.equalTo(_view.mas_right).offset(0);
            make.height.mas_equalTo(230);
        }];
        [_backImageView release];
        
        self.detail = [[UILabel alloc] init];
        _detail.font = [UIFont fontWithName:@"Helvetica" size:14];
        _detail.numberOfLines = 0;
        _detail.backgroundColor = [UIColor clearColor];
        [_view addSubview:_detail];
        [_detail mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_view.mas_left).offset(0);
            make.top.equalTo(_backImageView.mas_bottom).offset(15);
            make.right.equalTo(_view.mas_right).offset(0);
        }];
        [_detail release];
        
        self.dateImageView = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"shijian.png"];
        _dateImageView.image = image;
        [_view addSubview:_dateImageView];
        [_dateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_view.mas_left).offset(0);
            make.top.equalTo(_detail.mas_bottom).offset(18);
        }];
        [_dateImageView release];
        
        self.date = [[UILabel alloc] init];
        _date.font = [UIFont systemFontOfSize:12];
        _date.backgroundColor = [UIColor clearColor];
        [_view addSubview:_date];
        [_date mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_dateImageView.mas_right).offset(5);
            make.top.equalTo(_detail.mas_bottom).offset(18);
        }];
        [_date release];
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.0f], NSFontAttributeName, nil];
    if (self.detail.text == nil || [self.detail.text isEqualToString:@""] || self.detail.text.length == 0) {
        CGFloat height = 0;
        [self.detail setFrame:CGRectMake(self.detail.frame.origin.x, self.detail.frame.origin.y, self.detail.frame.size.width, height)];
        [self.dateImageView setFrame:CGRectMake(25, 220, 15, 15)];
        [self.date setFrame:CGRectMake(40, 220, CELL_WIDTH - 45, 15)];
        [self.view setFrame:CGRectMake(15, 0, CELL_WIDTH - 30, 200 + height + 40)];
    }else {
        if (IOS_VERSION > 7.0) {
            CGRect rect = [self.detail.text boundingRectWithSize:CGSizeMake(CELL_WIDTH - 30, 1000) options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading) attributes:dic context:nil];
            CGFloat height = rect.size.height;
            [self.detail setFrame:CGRectMake(self.detail.frame.origin.x, self.detail.frame.origin.y, self.detail.frame.size.width, height)];
            [self.dateImageView setFrame:CGRectMake(25, 220 + self.detail.frame.size.height, 15, 15)];
            [self.date setFrame:CGRectMake(45, 220 + self.detail.frame.size.height, CELL_WIDTH - 40, 15)];
            [self.view setFrame:CGRectMake(15, 0, CELL_WIDTH - 30, 200 + height + 40)];
        }else
        {
            CGFloat height = [self.detail.text sizeWithFont:[UIFont systemFontOfSize:15.0f] constrainedToSize:CGSizeMake(CELL_WIDTH - 30, 1000)].height;
            [self.detail setFrame:CGRectMake(self.detail.frame.origin.x, self.detail.frame.origin.y, self.detail.frame.size.width, height)];
            [self.dateImageView setFrame:CGRectMake(25, 220 + self.detail.frame.size.height, 15, 15)];
            [self.date setFrame:CGRectMake(45, 220 + self.detail.frame.size.height, CELL_WIDTH - 40, 15)];
            [self.view setFrame:CGRectMake(15, 0, CELL_WIDTH - 30, 200 + height + 40)];
        }
    }
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
